'''
Case Type： 故障&可靠性测试
Case Name： 集群业务处理过程中，修改主实例数据目录下pg_xlog目录权限，集群业务停止
Create At: 2020/07/20
@zou_jialiang0505328114
Descption:
步骤 1.启动主备数据库，判断数据库状态；
步骤 2.运行tpcc；
步骤 3.在业务执行过程中修改主节点pg_xlog权限，查看数据库状态；
步骤 4.恢复xlog权限，重启数据库；
'''
import os
import unittest
import time
from yat.test import macro
from yat.test import Node
from testcase.utils.CommonSH import *
from testcase.utils.Common import Common
from testcase.utils.Constant import Constant
from testcase.utils.Logger import Logger
from testcase.utils.ComThread import ComThread


class Deletaduit(unittest.TestCase):
    commonshpri = CommonSH('PrimaryDbUser')
    nodes_tuple = ('PrimaryDbUser', 'Standby1DbUser', 'Standby2DbUser')

    @RestartDbCluster(*nodes_tuple)
    def setUp(self):
        self.log = Logger()
        self.log.info('------------Opengauss_Reliability_Dbsys_Case199.py start------------')
        self.rootNode = Node()
        self.TPCC_PATH = macro.TPCC_PATH
        self.com = Common()
        self.Constant = Constant()

    def test_startdb(self):
        # 运行tpcc
        tpcc_thread = ComThread(self.com.startTPCC, args=(self.rootNode, self.TPCC_PATH,))
        tpcc_thread.setDaemon(True)
        tpcc_thread.start()

        time.sleep(5)

        # 修改主xlog目录权限
        chmodxlog = 'chmod 000 ' + macro.DB_INSTANCE_PATH + '/pg_xlog'
        self.log.info(chmodxlog)
        self.rootNode.sh(chmodxlog)

        tpcc_thread.join(int(macro.TPCC_RUN_MAX_TIME) * 2)
        tpcc_result = tpcc_thread.get_result()
        self.log.info(tpcc_result)
        self.assertIn(self.Constant.TPCC_ERROR, tpcc_result)

    def tearDown(self):
        # 恢复xlog目录权限，重启数据库
        chmodxlog = 'chmod 700 ' + macro.DB_INSTANCE_PATH + '/pg_xlog'
        self.log.info(chmodxlog)
        self.rootNode.sh(chmodxlog)

        self.commonshpri.startDbCluster()

        self.log.info(
            '------------------------------------reliability_dbsys199.py finish------------------------------------')
